package zxing.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.githang.statusbar.StatusBarCompat;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.ListResultBean;
import io.virtualapp.bean.ServiceItemBean;
import io.virtualapp.mylibrary.callback.BaseCallback;
import io.virtualapp.mylibrary.util.HttpUtils;
import io.virtualapp.mylibrary.util.ToastUtils;
import okhttp3.Request;
import okhttp3.Response;
import zxing.activity.adapter.SuggestListAdapter;

public class SuggestListActivity extends VActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.cl_)
    ConstraintLayout cl;
    @BindView(R.id.rc)
    RecyclerView rc;
    @BindView(R.id.pr_su_list)
    PullToRefreshLayout prSuList;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    Button btCreate;
    @BindView(R.id.iv_dis)
    ImageView ivDis;

    private SuggestListAdapter adapter;

    @SuppressLint("ResourceType")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.su_activity_suggest_list);


        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        adapter = new SuggestListAdapter(null);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rc.setLayoutManager(manager);
        rc.setAdapter(adapter);
        ivDis.setOnClickListener(view -> finish());

        prSuList.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                Updata(true);
            }

            @Override
            public void loadMore() {
                Updata(false);
            }
        });

        LayoutInflater inflater = this.getLayoutInflater();
        View view = inflater.inflate(R.layout.su_bottom_buton_layout, null);
        adapter.addFooterView(view);
        btCreate = view.findViewById(R.id.bt_bottom);

        btCreate.setOnClickListener(view1 -> {
            //ToastUtils.showShortToast("点击 了按钮");
            startActivityForResult(new Intent(SuggestListActivity.this, SuAddActivity.class), 121);

        });
        Updata(true);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                try {
                    int id = adapter.getData().get(position).getId();
                    Intent intent = new Intent(SuggestListActivity.this, SuDetailsActivity.class);
                    intent.putExtra("data", id);
                    startActivityForResult(intent, 121);

                } catch (Exception e) {
                    ToastUtils.showShortToast("当前item无效，请刷新重试");
                }

            }
        });
    }

    private int curent_page = 1;
    private int limt = 20;

    private void Updata(boolean isReFresh) {
        if (isReFresh) {
            curent_page = 1;
        } else {
            curent_page += 1;
        }

        HttpUtils.getInstance().postGetServices(curent_page, limt, new BaseCallback<ListResultBean<ServiceItemBean>>() {
            @Override
            public void onRequestBefore() {
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFailure(Request request, Exception e) {
                progressBar.setVisibility(View.GONE);
                prSuList.finishRefresh();
                prSuList.finishLoadMore();
            }

            @Override
            public void onSuccess(Response response, ListResultBean<ServiceItemBean> o) {
                progressBar.setVisibility(View.GONE);
                prSuList.finishRefresh();
                prSuList.finishLoadMore();
                if (isReFresh) {
                    if (o != null && o.getItems() != null && o.getItems().size() > 0) {

                    } else {
                        ToastUtils.showShortToast("当前没有反馈");
                    }
                    adapter.replaceData(o.getItems());
                } else {
                    if (o != null && o.getItems() != null && o.getItems().size() > 0) {
                        adapter.addData(o.getItems());
                    } else {
                        ToastUtils.showShortToast("没有更多数据了");
                    }

                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                progressBar.setVisibility(View.GONE);
                prSuList.finishRefresh();
                prSuList.finishLoadMore();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 121 && resultCode == RESULT_OK) {
            Updata(true);
        }
    }
}
